package com.atguigu.yygh.order.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.client.user.PatientFeignClient;
import com.atguigu.common.rabbit.constant.MqConst;
import com.atguigu.common.rabbit.service.RabbitService;
import com.atguigu.yygh.client.ServiceHospClient;
import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.enums.OrderStatusEnum;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.order.mapper.OrderInfoMapper;
import com.atguigu.yygh.order.service.OrderService;
import com.atguigu.yygh.order.service.WeixinService;
import com.atguigu.yygh.order.utils.HttpRequestHelper;
import com.atguigu.yygh.vo.hosp.ScheduleOrderVo;
import com.atguigu.yygh.vo.msm.MsmVo;
import com.atguigu.yygh.vo.order.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ybl
 * @version 1.9
 * @date 2021/5/23 20:03
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderService {

  @Autowired
  private ServiceHospClient serviceHospClient;

  @Autowired
  private PatientFeignClient patientFeignClient;

  @Autowired
  private RabbitService rabbitService;

  @Autowired
  private WeixinService weixinService;


  // 订单统计
  @Override
  public Map<String, Object> getCountMap(OrderCountQueryVo orderCountQueryVo) {
    Map<String, Object> map = new HashMap<>();

    List<OrderCountVo> orderCountVoList
        = baseMapper.selectOrderCount(orderCountQueryVo);
    //日期列表
    List<String> dateList
        =orderCountVoList.stream().map(OrderCountVo::getReserveDate).collect(Collectors.toList());
    //统计列表
    List<Integer> countList
        =orderCountVoList.stream().map(OrderCountVo::getCount).collect(Collectors.toList());
    map.put("dateList", dateList);
    map.put("countList", countList);
    return map;
  }

  // 创建订单
  @Override
  public Long saveOrder(String scheduleId, Long patientId) {
    // 就诊人信息
    Patient patient = patientFeignClient.getPatientOrder(patientId);
    if (null == patient) {
      throw new YyghException(ResultCodeEnum.PARAM_ERROR);
    }
    // 预约挂号信息
    ScheduleOrderVo scheduleOrderVo = serviceHospClient.getScheduleOrderVo(scheduleId);
    if (null == scheduleOrderVo) {
      throw new YyghException(ResultCodeEnum.PARAM_ERROR);
    }
    //当前时间不可以预约
    if (new DateTime(scheduleOrderVo.getStartTime()).isAfterNow() ||
    new DateTime(scheduleOrderVo.getEndTime()).isBeforeNow()) {
      throw new YyghException(ResultCodeEnum.TIME_NO);
    }

    // 签名信息
    SignInfoVo signInfoVo = serviceHospClient.getSignInfoVo(scheduleOrderVo.getHoscode());
    if (null == signInfoVo) {
      throw new YyghException(ResultCodeEnum.PARAM_ERROR);
    }

    // 可预约人数
    if (scheduleOrderVo.getAvailableNumber() <= 0) {
      throw new YyghException(ResultCodeEnum.NUMBER_NO);
    }

    OrderInfo orderInfo = new OrderInfo();
    BeanUtils.copyProperties(scheduleOrderVo,orderInfo);
    String outTradeNo = System.currentTimeMillis()+""+ new Random().nextInt(100);
    orderInfo.setOutTradeNo(outTradeNo);
    orderInfo.setScheduleId(scheduleId);
    orderInfo.setUserId(patient.getUserId());
    orderInfo.setPatientId(patientId);
    orderInfo.setPatientName(patient.getName());
    orderInfo.setPatientPhone(patient.getPhone());
    orderInfo.setOrderStatus(OrderStatusEnum.UNPAID.getStatus());
    orderInfo.setAmount(new BigDecimal(100));
    this.save(orderInfo);

    //调用医院接口，实现预约挂号操作
    //设置调用医院接口需要参数，参数放到map集合
    Map<String, Object> paramMap = new HashMap<>();
    paramMap.put("hoscode",orderInfo.getHoscode());
    paramMap.put("depcode",orderInfo.getDepcode());
    paramMap.put("hosScheduleId",orderInfo.getScheduleId());
    paramMap.put("reserveDate",new DateTime(orderInfo.getReserveDate()).toString("yyyy-MM-dd"));
    paramMap.put("reserveTime", orderInfo.getReserveTime());
    paramMap.put("amount",orderInfo.getAmount());
    paramMap.put("name", patient.getName());
    paramMap.put("certificatesType",patient.getCertificatesType());
    paramMap.put("certificatesNo", patient.getCertificatesNo());
    paramMap.put("sex",patient.getSex());
    paramMap.put("birthdate", patient.getBirthdate());
    paramMap.put("phone",patient.getPhone());
    paramMap.put("isMarry", patient.getIsMarry());
    paramMap.put("provinceCode",patient.getProvinceCode());
    paramMap.put("cityCode", patient.getCityCode());
    paramMap.put("districtCode",patient.getDistrictCode());
    paramMap.put("address",patient.getAddress());
    //联系人
    paramMap.put("contactsName",patient.getContactsName());
    paramMap.put("contactsCertificatesType", patient.getContactsCertificatesType());
    paramMap.put("contactsCertificatesNo",patient.getContactsCertificatesNo());
    paramMap.put("contactsPhone",patient.getContactsPhone());
    paramMap.put("timestamp", HttpRequestHelper.getTimestamp());
    // sign
    paramMap.put("sign", MD5.encrypt(signInfoVo.getSignKey()));


    JSONObject result = HttpRequestHelper.sendRequest(paramMap, signInfoVo.getApiUrl()+"/order/submitOrder");

    // if(result.getInteger("code") == 200) {
    //   JSONObject jsonObject = result.getJSONObject("data");
    //   //预约记录唯一标识（医院预约记录主键）
    //   String hosRecordId = jsonObject.getString("hosRecordId");
    //   //预约序号
    //   Integer number = jsonObject.getInteger("number");;
    //   //取号时间
    //   String fetchTime = jsonObject.getString("fetchTime");;
    //   //取号地址
    //   String fetchAddress = jsonObject.getString("fetchAddress");
    //   //更新订单
    //   orderInfo.setHosRecordId(hosRecordId);
    //   orderInfo.setNumber(number);
    //   orderInfo.setFetchTime(fetchTime);
    //   orderInfo.setFetchAddress(fetchAddress);
    //   baseMapper.updateById(orderInfo);
      //排班可预约数
      // Integer reservedNumber = jsonObject.getInteger("reservedNumber");
      // //排班剩余预约数
      // Integer availableNumber = jsonObject.getInteger("availableNumber");
      //发送mq信息更新号源和短信通知
      //发送mq信息更新号源
      OrderMqVo orderMqVo = new OrderMqVo();
      orderMqVo.setScheduleId(scheduleId);
      orderMqVo.setReservedNumber(100);
      orderMqVo.setAvailableNumber(100);

      //短信提示
      MsmVo msmVo = new MsmVo();
      msmVo.setPhone(orderInfo.getPatientPhone());
      msmVo.setTemplateCode("SMS_205617460");
      // 保留日期
      String reserveDate =
          new DateTime(orderInfo.getReserveDate()).toString("yyyy-MM-dd")
              + (orderInfo.getReserveTime()==0 ? "上午": "下午");
      Map<String,Object> param = new HashMap<String,Object>(){{
        put("title", orderInfo.getHosname()+"|"+orderInfo.getDepname()+"|"+orderInfo.getTitle());
        put("amount", orderInfo.getAmount());
        put("reserveDate", reserveDate);
        put("name", orderInfo.getPatientName());
        put("quitTime", new DateTime(new Date()).toString("yyyy-MM-dd HH:mm"));
      }};
      msmVo.setParam(param);

      orderMqVo.setMsmVo(msmVo);
      rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_ORDER, MqConst.ROUTING_ORDER, orderMqVo);
    // } else {
    //   throw new YyghException(result.getString("message"), ResultCodeEnum.FAIL.getCode());
    // }
    return orderInfo.getId();
  }

  //根据订单id查询订单详情
  @Override
  public OrderInfo getOrder(String orderId) {
    OrderInfo orderInfo = baseMapper.selectById(orderId);
    this.packOrderInfo(orderInfo);
    return orderInfo;
  }

  // 订单详情
  @Override
  public Map<String,Object> show(Long id) {
    Map<String,Object> map = new HashMap<>();
    OrderInfo orderInfo = baseMapper.selectById(id);
    this.packOrderInfo(orderInfo);
    Patient patient = patientFeignClient.getPatientOrder(orderInfo.getPatientId());
    map.put("orderInfo",orderInfo);
    map.put("patient",patient);
    return map;
  }

  // 查询所有已经预约未就诊人信息
  @Override
  public void patientTips() {
    QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("reserve_date",new DateTime().toString("yyyy-MM-dd"));
    List<OrderInfo> orderInfos = baseMapper.selectList(queryWrapper);
    for(OrderInfo orderInfo : orderInfos) {
      //短信提示
      MsmVo msmVo = new MsmVo();
      msmVo.setPhone(orderInfo.getPatientPhone());
      String reserveDate = new DateTime(orderInfo.getReserveDate()).toString("yyyy-MM-dd") + (orderInfo.getReserveTime()==0 ? "上午": "下午");
      Map<String,Object> param = new HashMap<String,Object>(){{
        put("title", orderInfo.getHosname()+"|"+orderInfo.getDepname()+"|"+orderInfo.getTitle());
        put("reserveDate", reserveDate);
        put("name", orderInfo.getPatientName());
      }};
      msmVo.setParam(param);
      rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_MSM, MqConst.ROUTING_MSM_ITEM, msmVo);
    }

  }

  // 取消订单
  @Override
  public Boolean cancelOrder(Long orderId) {
    OrderInfo orderInfo = this.getById(orderId);
//当前时间大约退号时间，不能取消预约
//     DateTime quitTime = new DateTime(orderInfo.getQuitTime());
//     if(quitTime.isBeforeNow()) {
//       throw new YyghException(ResultCodeEnum.CANCEL_ORDER_NO);
//     }
    SignInfoVo signInfoVo = serviceHospClient.getSignInfoVo(orderInfo.getHoscode());
    if(null == signInfoVo) {
      throw new YyghException(ResultCodeEnum.PARAM_ERROR);
    }
    Map<String, Object> reqMap = new HashMap<>();
    reqMap.put("hoscode",orderInfo.getHoscode());
    reqMap.put("hosRecordId",orderInfo.getHosRecordId());
    reqMap.put("timestamp", HttpRequestHelper.getTimestamp());
    String sign = HttpRequestHelper.getSign(reqMap, signInfoVo.getSignKey());
    reqMap.put("sign", sign);

    JSONObject result = HttpRequestHelper.sendRequest(reqMap, signInfoVo.getApiUrl()+"/order/updateCancelStatus");

    // if(result.getInteger("code") != 200) {
    //   throw new YyghException(result.getString("message"), ResultCodeEnum.FAIL.getCode());
    // } else {
//是否支付 退款
      if(orderInfo.getOrderStatus().intValue() == OrderStatusEnum.PAID.getStatus().intValue()) {
//已支付 退款
        boolean isRefund = weixinService.refund(orderId);
        if(!isRefund) {
          throw new YyghException(ResultCodeEnum.CANCEL_ORDER_FAIL);
        }
      // }
//更改订单状态
      orderInfo.setOrderStatus(OrderStatusEnum.CANCLE.getStatus());
      this.updateById(orderInfo);
//发送mq信息更新预约数 我们与下单成功更新预约数使用相同的mq信息，不设置可预约数与剩余预约数，接收端可预约数减1即可
      OrderMqVo orderMqVo = new OrderMqVo();
      orderMqVo.setScheduleId(orderInfo.getScheduleId());
//短信提示
      MsmVo msmVo = new MsmVo();
      msmVo.setPhone(orderInfo.getPatientPhone());
      msmVo.setTemplateCode("SMS_194640722");
      String reserveDate = new DateTime(orderInfo.getReserveDate()).toString("yyyy-MM-dd") + (orderInfo.getReserveTime()==0 ? "上午": "下午");
      Map<String,Object> param = new HashMap<String,Object>(){{
        put("title", orderInfo.getHosname()+"|"+orderInfo.getDepname()+"|"+orderInfo.getTitle());
        put("reserveDate", reserveDate);
        put("name", orderInfo.getPatientName());
      }};
      msmVo.setParam(param);
      orderMqVo.setMsmVo(msmVo);
      rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_ORDER, MqConst.ROUTING_ORDER, orderMqVo);
    }
    return true;
  }


  //订单列表（条件查询带分页下`）
  @Override
  public IPage<OrderInfo> selectPage(Page<OrderInfo> pageParam, OrderQueryVo orderQueryVo) {
    //orderQueryVo获取条件值
    String name = orderQueryVo.getKeyword(); //医院名称
    Long patientId = orderQueryVo.getPatientId(); //就诊人名称
    String orderStatus = orderQueryVo.getOrderStatus(); //订单状态
    String reserveDate = orderQueryVo.getReserveDate();//安排时间
    String createTimeBegin = orderQueryVo.getCreateTimeBegin();
    String createTimeEnd = orderQueryVo.getCreateTimeEnd();
    //对条件值进行非空判断
    QueryWrapper<OrderInfo> wrapper = new QueryWrapper<>();
    if(!StringUtils.isEmpty(name)) {
      wrapper.like("hosname",name);
    }
    if(!StringUtils.isEmpty(patientId)) {
      wrapper.eq("patient_id",patientId);
    }
    if(!StringUtils.isEmpty(orderStatus)) {
      wrapper.eq("order_status",orderStatus);
    }
    if(!StringUtils.isEmpty(reserveDate)) {
      wrapper.ge("reserve_date",reserveDate);
    }
    if(!StringUtils.isEmpty(createTimeBegin)) {
      wrapper.ge("create_time",createTimeBegin);
    }
    if(!StringUtils.isEmpty(createTimeEnd)) {
      wrapper.le("create_time",createTimeEnd);
    }
    //调用mapper的方法
    IPage<OrderInfo> pages = baseMapper.selectPage(pageParam, wrapper);
    //编号变成对应值封装
    pages.getRecords().stream().forEach(item -> {
      this.packOrderInfo(item);
    });
    return pages;

  }

  private void packOrderInfo(OrderInfo orderInfo) {
    orderInfo.getParam().put("orderStatusString", OrderStatusEnum.getStatusNameByStatus(orderInfo.getOrderStatus()));
  }

}
